using System;
using System.Data;
using System.Web;
using BEGDC.CC.BL.CCFlow.CR;
using BEGDC.CC.BL.Common;
using BEGDC.CC.BL.Common.Query;
using BEGDC.CC.Entity;
using BEGDC.Utility.Castle;
using NHibernate;

namespace BEGDC.CC.ExcelTool
{
	/// <summary>
	/// Summary description for ExportCOForm.
	/// </summary>
public class ExportEVForm:ICCExcelExport
{
	private string templateName = HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["EVFormTemplate"]);
	private string targetFileName = HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["ExportPath"]);
	private string fileName;

	private IExcelExport excelExport;

	public ExportEVForm()
	{
		this.excelExport = new ExcelExportCommom();
		
	}
	public ExportEVForm(IExcelExport excelExport)
	{
		this.excelExport = excelExport;

	}
	private void GenerateFileName()
	{
		DateTime tmpTime=DateTime.Now;
		this.fileName=tmpTime.Millisecond.ToString();
		this.fileName+=tmpTime.Second.ToString();
		this.fileName+=tmpTime.Minute.ToString();
		this.fileName+="_EVForm.xls";
	}

	public void GenerateEVForm(int appId )
	{
		try
		{
			this.excelExport.Open( templateName );

			BEGDC.CC.BL.Common.Query.IQuery queryService =  AppContainer.Instance[ServiceConst.QueryService] as BEGDC.CC.BL.Common.Query.IQuery;
					
//			this.fileName = appId.ToString()+ "_" + this.fileName;
//			this.excelExport.ExportAs(this.targetFileName + this.fileName);
			//Fill in application information
			GenerateAppInfo(queryService, appId );
			
			//Fill in customer information
			//GenerateVistingDetails( queryService, appId );
			

			//Fill CustomerAttendee
			GenerateCustomerAttendee( queryService, appId );
			
			//Fill ASBAttendee
			GenerateASBAttendee( queryService, appId );

			this.fileName=this.fileName.Replace("/","").Replace("\\","").Replace("'","").Replace("\"","");
			this.fileName=this.targetFileName + this.fileName;

			this.excelExport.ExportAs(this.fileName);
		}
		catch(Exception ex)
		{
			BEGDC.Utility.ExceptionManagement.GDCException exp=new BEGDC.Utility.ExceptionManagement.GDCException("export_0001",ex.Message,ex.InnerException);
			throw exp;
		}
		finally
		{
			this.excelExport.Close();
		}
	}

	private void GenerateAppInfo(BEGDC.CC.BL.Common.Query.IQuery queryService, int appId )
	{
		Application app = queryService.GetAppById( appId );
		
		if( app == null )
			return;

		//set AppNo
		Excel.Range rngTemp = this.excelExport.MyApp.get_Range("AppNo", Type.Missing );
		this.excelExport.MyApp.get_Range( rngTemp.Cells[1,1], rngTemp.Cells[1,1]).Value2 = app.AppNo;
		this.fileName =app.AppNo + "_" + this.fileName;

		//set applicant information
		rngTemp = this.excelExport.MyApp.get_Range("ApplicantInfo", Type.Missing );
	
		this.excelExport.MyApp.get_Range( rngTemp.Cells[1,3], rngTemp.Cells[1,3]).Value2 = app.RequesterNameEN;

		this.excelExport.MyApp.get_Range( rngTemp.Cells[2,3], rngTemp.Cells[2,3]).Value2 = app.CATBranch;
//		if (app.DepartmentName!=null)
//		{
//			this.excelExport.MyApp.get_Range( rngTemp.Cells[3,3], rngTemp.Cells[3,3]).Value2 = app.DepartmentName.DepartmentCode;
//		}
		this.excelExport.MyApp.get_Range( rngTemp.Cells[3,3], rngTemp.Cells[3,3]).Value2 = app.Account;
		if (app.LegalEntity != null)
			this.excelExport.MyApp.get_Range( rngTemp.Cells[4,3], rngTemp.Cells[4,3]).Value2 = app.LegalEntity.LegalEntityName;


		
		//set visiting details
		
		rngTemp = this.excelExport.MyApp.get_Range("VistingDetails", Type.Missing );
		
		if (app.RequestedDateFrom != Nullables.NullableDateTime.Default)
		{
			this.excelExport.MyApp.get_Range( rngTemp.Cells[1,3], rngTemp.Cells[1,3]).Value2 = app.RequestedDateFrom.Value.ToString("yyyy-MM-dd");
			this.excelExport.MyApp.get_Range( rngTemp.Cells[1,5], rngTemp.Cells[1,5]).Value2 = app.RequestedDateFrom.Value.ToString("HH:mm");
		}
		if (app.AlternateDateFrom != Nullables.NullableDateTime.Default)
		{
			this.excelExport.MyApp.get_Range( rngTemp.Cells[2,3], rngTemp.Cells[2,3]).Value2 = app.AlternateDateFrom.Value.ToString("yyyy-MM-dd");
			this.excelExport.MyApp.get_Range( rngTemp.Cells[2,5], rngTemp.Cells[2,5]).Value2 = app.AlternateDateFrom.Value.ToString("HH:mm");
		}
		
		BEGDC.CC.Entity.CustomerInfo custInfo=queryService.GetCustomerInfo(appId);
		if (custInfo!=null)
		{
			this.excelExport.MyApp.get_Range( rngTemp.Cells[3,3], rngTemp.Cells[3,3]).Value2 = custInfo.Company;
			this.excelExport.MyApp.get_Range( rngTemp.Cells[3,5], rngTemp.Cells[3,5]).Value2 = custInfo.Venue;
			this.excelExport.MyApp.get_Range( rngTemp.Cells[4,3], rngTemp.Cells[4,3]).Value2 = custInfo.VisitObjective;
		}
		
		this.excelExport.MyApp.get_Range( rngTemp.Cells[5,3], rngTemp.Cells[5,3]).Value2 = app.Activity;
		
	}
	private void GenerateCustomerAttendee(BEGDC.CC.BL.Common.Query.IQuery queryService, int appId )
	{
		DataTable custAttendee = queryService.QueryCustomerAttendeeByAppId( appId );
		Excel.Range rngTemp = this.excelExport.MyApp.get_Range("CustomerAttendee", Type.Missing);
		int rowCount=2;
		while (custAttendee.Rows.Count> rowCount)
		{
			this.excelExport.MyApp.get_Range(rngTemp.Cells[2,1],rngTemp.Cells[2,6]).EntireRow.Copy(Type.Missing);
			this.excelExport.MyApp.get_Range(rngTemp.Cells[2,1],rngTemp.Cells[2,6]).EntireRow.Insert(Excel.XlInsertShiftDirection.xlShiftDown, false);
			rowCount++;
		}

		rngTemp = this.excelExport.MyApp.get_Range("CustomerAttendee", Type.Missing);

		if( custAttendee != null && custAttendee.Rows.Count > 0 )
		{
			for( int i=0; i<custAttendee.Rows.Count; i++)
			{
				if( custAttendee.Rows[i]["NameEN"] != System.DBNull.Value )
				{
					if (custAttendee.Rows[i]["NameEN"].ToString().Trim()!= "" && custAttendee.Rows[i]["NameEN"].ToString()!=string.Empty )
						this.excelExport.MyApp.get_Range( rngTemp.Cells[i+1,1], rngTemp.Cells[i+1,1]).Value2 = custAttendee.Rows[i]["NameEN"].ToString();
					else
					{
						if (custAttendee.Rows[i]["NameCN"] != System.DBNull.Value )
							this.excelExport.MyApp.get_Range( rngTemp.Cells[i+1,1], rngTemp.Cells[i+1,1]).Value2 = custAttendee.Rows[i]["NameCN"].ToString();
					}
				}
				else
				{
					if (custAttendee.Rows[i]["NameCN"] != System.DBNull.Value )
						this.excelExport.MyApp.get_Range( rngTemp.Cells[i+1,1], rngTemp.Cells[i+1,1]).Value2 = custAttendee.Rows[i]["NameEN"].ToString();
				}	
			
				if( custAttendee.Rows[i]["CompanyEN"] != System.DBNull.Value )
					this.excelExport.MyApp.get_Range( rngTemp.Cells[i+1,2], rngTemp.Cells[i+1,2]).Value2 = custAttendee.Rows[i]["CompanyEN"].ToString();
				
				if( custAttendee.Rows[i]["TitleEN"] != System.DBNull.Value )
					this.excelExport.MyApp.get_Range( rngTemp.Cells[i+1,3], rngTemp.Cells[i+1,3]).Value2 = custAttendee.Rows[i]["TitleEN"].ToString();

				if( custAttendee.Rows[i]["Responsibility"] != System.DBNull.Value )
					this.excelExport.MyApp.get_Range( rngTemp.Cells[i+1,4], rngTemp.Cells[i+1,4]).Value2 = custAttendee.Rows[i]["Responsibility"].ToString();
				
				if( custAttendee.Rows[i]["Affects"] != System.DBNull.Value )
					this.excelExport.MyApp.get_Range( rngTemp.Cells[i+1,5], rngTemp.Cells[i+1,5]).Value2 = custAttendee.Rows[i]["Affects"].ToString();

				if( custAttendee.Rows[i]["Remark"] != System.DBNull.Value )
					this.excelExport.MyApp.get_Range( rngTemp.Cells[i+1,6], rngTemp.Cells[i+1,6]).Value2 = custAttendee.Rows[i]["Remark"].ToString();
					
			}
		}

	}

	private void GenerateASBAttendee( BEGDC.CC.BL.Common.Query.IQuery queryService, int appId)
	{
		DataTable alAttendee = queryService.QueryALAttendeeByAppId( appId );
		Excel.Range rngTemp = this.excelExport.MyApp.get_Range("ProposedEV", Type.Missing);
		int rowCount=2;
		while (alAttendee.Rows.Count> rowCount)
		{
			this.excelExport.MyApp.get_Range(rngTemp.Cells[2,1],rngTemp.Cells[2,6]).EntireRow.Copy(Type.Missing);
			this.excelExport.MyApp.get_Range(rngTemp.Cells[2,1],rngTemp.Cells[2,6]).EntireRow.Insert(Excel.XlInsertShiftDirection.xlShiftDown, false);
			rowCount++;
		}

		rngTemp = this.excelExport.MyApp.get_Range("ProposedEV", Type.Missing);

		if( alAttendee != null && alAttendee.Rows.Count > 0 )
		{
			for( int i=0; i<alAttendee.Rows.Count; i++)
			{
				if( alAttendee.Rows[i]["NameEN"] != System.DBNull.Value)
				{ 
					if (alAttendee.Rows[i]["NameEN"].ToString().Trim() != "" && alAttendee.Rows[i]["NameEN"].ToString()!=string.Empty)
						this.excelExport.MyApp.get_Range( rngTemp.Cells[i+1,1], rngTemp.Cells[i+1,1]).Value2 = alAttendee.Rows[i]["NameEN"].ToString();
					else
					{
						if (alAttendee.Rows[i]["NameCN"] != System.DBNull.Value)
							this.excelExport.MyApp.get_Range( rngTemp.Cells[i+1,1], rngTemp.Cells[i+1,1]).Value2 = alAttendee.Rows[i]["NameCN"].ToString();
					
					}
				}
				else
				{
					if (alAttendee.Rows[i]["NameCN"] != System.DBNull.Value)
						this.excelExport.MyApp.get_Range( rngTemp.Cells[i+1,1], rngTemp.Cells[i+1,1]).Value2 = alAttendee.Rows[i]["NameCN"].ToString();
					
				}				
				if( alAttendee.Rows[i]["Title"] != System.DBNull.Value )
					this.excelExport.MyApp.get_Range( rngTemp.Cells[i+1,4], rngTemp.Cells[i+1,3]).Value2 = alAttendee.Rows[i]["Title"].ToString();					
			}
		}
	}

	#region ICCExcelExport Members

	public void Export(System.Web.UI.Page page, int id)
	{			
		//generate filename randomly
		GenerateFileName();
		this.GenerateEVForm( id );

		this.excelExport.Export2Client(page, this.fileName, "EVForm.xls");
	}

	#endregion
	}
}
